Skip to content

fix: blockwise _twist! to support twist and non-inplace twist! - #94

Merged
lkdvos merged 1 commit into
mainfrom
bd/twist
Nov 27, 2026
Merged

lkdvos merged 1 commit into
mainfrom
bd/twist

Conversation

@borisdevos

Copy link
Copy Markdown
Member

Since TensorKit v0.17.4 (QuantumKitHub/TensorKit.jl#599), twist(t, inds) calls the new twist!(tdst, tsrc, inds, α, β, ...), which unwraps adjoints and calls the internal _twist!. BlockTensorKit only overloaded the in-place twist!(t, inds), so for block tensors twist and the new twist! ended up in TensorKit's generic kernel.
What went wrong though, is that for a sparse block tensor the subblock of a missing block is just a temporary zero array, so twist of a sparse block tensor returns zeros, and twist! into a sparse destination drops blocks it didn't already have.
What worked but was slow due to the generic fallback in TK was dense block tensors (by a factor 5 at least, up to 20).

This PR overloads TK._twist! blockwise, like _braid!, with one difference: in _twist! the twist mask refers to the legs of the source.

I added a test covering twist and twist!(tdst, tsrc, ...) with dense/sparse source and destination, with and without adjoint, which currently fails on main.

transpose! could get the same treatment through _transpose!, but BlockTensorKit's transpose! already supports this except for a AdjointTensorMap(D) destination. That case is wrong for a sparse D and slow for a dense one, so I'll look into that as well.

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/tensors/indexmanipulations.jl 64.94% <100.00%> (+9.68%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos
lkdvos merged commit 34a00be into main Nov 27, 2026
28 checks passed
@lkdvos
lkdvos deleted the bd/twist branch November 27, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants